import {
	mapState,
	mapMutations,
	mapGetters
} from 'vuex';
import $Config from '@/common/config/config.js'
// const dbTemp = uniCloud.database()
export default {
	filters: {
		// 获取日期的号
		getWeekDate(date){
			date=date.toString()
			return date.slice(6, 8)
		},
		// 根据日期获取周几
		getweekday(date) {
			date=date.toString()
			date = date.slice(0, 4) + '-' + date.slice(4)
			date = date.slice(0, 7) + '-' + date.slice(7)
			var weekArray = new Array("日", "一", "二", "三", "四", "五", "六");
			var week = weekArray[new Date(date).getDay()]; //注意此处必须是先new一个Date
			return week;
		},
		getLevelByKey(value) {
			return $Config.levelEnum[value].name
		},
		firstLoad(value) {
			return value ? value : 'load...'
		},
		formatOne(value) {
			if (!value) return ''
			return value.substring(0, 1)
		},
		formatYMD(value) {
			if (!value) return ''
			let year = value.toString().substring(0, 4)
			let month = value.toString().substring(4, 6)
			let day = value.toString().substring(6, 8)
			return `${year}年${month}月${day}日`
		},
		formatDate(value) {
			var d = new Date(value);
			var year = d.getFullYear(); //取得4位数的年份
			var month = d.getMonth() + 1; //取得日期中的月份，其中0表示1月，11表示12月
			var date = d.getDate(); //返回日期月份中的天数（1到31）
			return year + "-" + month + "-" + date
		}
	},
	computed: {
		...mapState({
			userInfo: state => state.user.userInfo,
			student: state => state.user.student,
			teacher: state => state.user.teacher,
			ShareData: state => state.share.ShareData,
			logo: state => state.user.logo,
			hasLogin: state => state.user.hasLogin
		}),
		// ...mapGetters(['isStudent', 'isTeacher', 'isAdmin'])
	},
	onShow() {
		let page = getCurrentPages()
		if (page.length && page[0].route != 'pages/tabbars/index/index') {
			if (!this.hasLogin) {
				this.logout()
			}
		}
	},
	methods: {
		...mapMutations(['vModel', 'login', 'logout', 'initRole', 'initShareData', 'initLogo']),
		showT(title, icon = 'none') {
			uni.showToast({
				title: title,
				icon: icon
			});
		},
		formatYMDF(value) {
			if (!value) return ''
			let year = value.toString().substring(0, 4)
			let month = value.toString().substring(4, 6)
			let day = value.toString().substring(6, 8)
			return `${year}年${month}月${day}日`
		},
		formatTimeF(start,end) {
			if (!start||!end) return ''
			return `${start}:00~${end}:00`
		},
		// async _initST(id, collection) {
		// 	let res = await dbTemp.collection(collection).where(`user_id=="${id}"`).get()
		// 	if (!res.success) return this.showT('获取身份失败')
		// 	this.initRole({
		// 		key: collection,
		// 		value: res.result.data[0]
		// 	})
		// },
		getDateIndex() {
			let dateList = this.$Utils.getDateWeek()
			let dateIndex = parseInt(dateList.dateWeek[0].year.toString() + dateList.dateWeek[0].date.replace('.', ''))
			return dateIndex
		},
		getWeixinCode() {
			return new Promise((resolve, reject) => {
				// #ifdef APP-PLUS
				weixinAuthService.authorize(function(res) {
					resolve(res.code)
				}, function(err) {
					console.log(err)
					reject(new Error('微信登录失败'))
				});
				// #endif
				// #ifdef MP-WEIXIN
				uni.login({
					provider: 'weixin',
					success: (res) => {
						resolve(res.code)
					},
					fail(err) {
						console.log(err)
						reject(new Error('微信登录失败'))
					}
				})
				// #endif
			})
		},
		isArrayAndSplit(array, key) {
			if (!Array.isArray(array)) {
				if (array.indexOf(",") !== -1) {
					return array.split(",")
				}
				if (array.indexOf("，") !== -1) {
					return array.split("，")
				}
				if (array.indexOf(" ") !== -1) {
					return array.split(" ")
				}
				return [array]
			}
			return array
		},
		navigateTo(url) {
			uni.navigateTo({
				url: url
			})
		},
		validate(key, rules, value) {
			let check = true
			rules[key].forEach(v => {
				if (!v.rule.test(value)) {
					uni.showToast({
						title: v.msg,
						icon: 'none'
					})
					check = false
				}
			})
			return check;
		},
		getNowFormatDate() {
			var date = new Date();
			var seperator1 = "-";
			var year = date.getFullYear();
			var month = date.getMonth() + 1;
			var strDate = date.getDate();
			if (month >= 1 && month <= 9) {
				month = "0" + month;
			}
			if (strDate >= 0 && strDate <= 9) {
				strDate = "0" + strDate;
			}
			var currentdate = year + seperator1 + month + seperator1 + strDate;
			return currentdate;
		}
	},
	onShareTimeline() {
		wx.showShareMenu({
			withShareTicket: true,
			menus: ['shareAppMessage', 'shareTimeline']
		})
		return {
			title: this.ShareData.title,
			query: this.ShareData.query,
			imageUrl: this.ShareData.imageUrl,
			desc: this.ShareData.desc
		}
	},
	// 分享
	onShareAppMessage() {
		return {
			title: this.ShareData.title,
			path: this.ShareData.path + this.ShareData.query,
			imageUrl: this.ShareData.imageUrl,
			desc: this.ShareData.desc
		}
	}
}
